/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.gui.frames;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

import sshtunneler.packager.data.ScriptVariables;

public class ShowScriptVariables extends MyInternalFrame {
	private static final long serialVersionUID = 2605048603088071098L;

	public ShowScriptVariables(MyInternalFrameActionDispatcherFrame dispatcher) {
		super(dispatcher);
		this.initComponents();
		dispatcher.add_script_variable_listener(this);
	}

	private void initComponents() {

		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.jTable1 = new javax.swing.JTable();
		this.jPanel1 = new javax.swing.JPanel();
		this.txt_newvarname = new javax.swing.JTextField();
		this.txt_newvarvalue = new javax.swing.JTextField();
		this.cmd_add = new javax.swing.JButton();

		this.setClosable(true);
		this.setIconifiable(true);
		this.setMaximizable(true);
		this.setResizable(true);
		this.setTitle("Script variables");
		this.setName("Form");
		this.setPreferredSize(new java.awt.Dimension(420, 200));

		this.jScrollPane1.setName("jScrollPane1");

		this.jTable1.setModel(new MyTableModel());
		this.jTable1.setName("jTable1");
		this.jScrollPane1.setViewportView(this.jTable1);

		this.getContentPane().add(this.jScrollPane1,
				java.awt.BorderLayout.CENTER);

		this.jPanel1.setName("jPanel1");

		this.txt_newvarname.setText("name");
		this.txt_newvarname.setMinimumSize(new java.awt.Dimension(80, 20));
		this.txt_newvarname.setName("txt_newvarname");
		this.txt_newvarname.setPreferredSize(new java.awt.Dimension(90, 20));
		this.jPanel1.add(this.txt_newvarname);

		this.txt_newvarvalue.setText("value");
		this.txt_newvarvalue.setMinimumSize(new java.awt.Dimension(80, 20));
		this.txt_newvarvalue.setName("txt_newvarvalue");
		this.txt_newvarvalue.setPreferredSize(new java.awt.Dimension(200, 20));
		this.jPanel1.add(this.txt_newvarvalue);

		this.cmd_add.setText("add");
		this.cmd_add.setName("cmd_add");
		this.cmd_add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ShowScriptVariables.this.cmd_addActionPerformed(evt);
			}
		});
		this.jPanel1.add(this.cmd_add);

		this.getContentPane().add(this.jPanel1, java.awt.BorderLayout.PAGE_END);

		this.pack();
	}

	private void cmd_addActionPerformed(java.awt.event.ActionEvent evt) {
		String varname = this.txt_newvarname.getText();
		String value = this.txt_newvarvalue.getText();

		if (ScriptVariables.inst.hasVariable(varname)) {
			JOptionPane.showMessageDialog(this,
					"A variable with this name already exists.");
			return;
		}
		ScriptVariables.inst.setVariableValue(varname, value);
		this.dispatcher.fire_script_variable_changed();
	}

	private javax.swing.JButton cmd_add;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField txt_newvarname;
	private javax.swing.JTextField txt_newvarvalue;

	public void refresh() {
		((AbstractTableModel) this.jTable1.getModel()).fireTableDataChanged();
	}

	class MyTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 1553923914699578380L;
		private String[] columnNames = { "Name", "Value" };

		public int getColumnCount() {
			return this.columnNames.length;
		}

		public int getRowCount() {
			return ScriptVariables.inst.allVariables().size();
		}

		@Override
		public String getColumnName(int col) {
			return this.columnNames[col];
		}

		public String variableAt(int row) {
			return ScriptVariables.inst.allVariables().get(row);
		}

		public Object getValueAt(int row, int col) {
			if (col == 0) {
				return this.variableAt(row);
			} else {
				return ScriptVariables.inst.getVariableValue(this
						.variableAt(row));
			}
		}

		@Override
		public Class<?> getColumnClass(int c) {
			return String.class;
		}

		@Override
		public boolean isCellEditable(int row, int col) {
			// Note that the data/cell address is constant,
			// no matter where the cell appears onscreen.
			if (col == 0) {
				return false;
			} else {
				return ScriptVariables.inst.changeable(this.variableAt(row));
			}
		}

		@Override
		public void setValueAt(Object value, int row, int col) {
			String val = (String) value;
			if (val.equals("")) {
				ScriptVariables.inst.removeVariable(this.variableAt(row));
			} else {
				ScriptVariables.inst
						.setVariableValue(this.variableAt(row), val);
			}
			ShowScriptVariables.this.dispatcher.fire_script_variable_changed();
		}
	}
}
